package com.wfh.yudada.scroing;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wfh.yudada.model.dto.question.QuestionContentDTO;
import com.wfh.yudada.model.entity.App;
import com.wfh.yudada.model.entity.Question;
import com.wfh.yudada.model.entity.ScoringResult;
import com.wfh.yudada.model.entity.UserAnswer;
import com.wfh.yudada.model.enums.AppScoreStrategyEnum;
import com.wfh.yudada.model.vo.QuestionVO;
import com.wfh.yudada.service.QuestionService;
import com.wfh.yudada.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @Author FengHuan Wang
 * @Date 2025/5/23 12:27
 * @Version 1.0
 */
@ScoringStrategyConfig(appType = 0, scoringStrategy = 0)
public class CunstomScoreScoringStrategy implements ScoringStrategy {

    @Resource
    private QuestionService questionService;

    @Resource
    private ScoringResultService scoringResultService;

    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        // 根据题目id查询题目结果信息
        Long appId = app.getId();
        questionService.getOne(Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, appId));
        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, appId)
                        .orderByAsc(ScoringResult::getResultScoreRange)
        );
        // 统计用户的总得分
        // 统计用户每个选项的个数
        int totalScore = 0;
        QuestionVO questionVO = new QuestionVO();
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        // 遍历每个评分结果，计算最高分
        // 遍历题目列表
        for (QuestionContentDTO questionContentDTO : questionContent) {
            // 遍历答案列表
            for (String choice : choices) {
                // 遍历题目中的选项
                for (QuestionContentDTO.Option option : questionContentDTO.getOptions()) {
                    // 如果答案和选项的key匹配
                    if (option.getKey().equals(choice)) {
                        int score = Optional.of(option.getScore()).orElse(0);
                        totalScore += score;
                    }
                }
            }
        }
        // 遍历得分结果，找到第一个用户的分数大于最大分数的用户
        ScoringResult maxScoringResult = scoringResultList.get(0);
        // 遍历
        for (ScoringResult scoringResult : scoringResultList) {
            if (totalScore >= scoringResult.getResultScoreRange()) {
                maxScoringResult = scoringResult;
                break;
            }
        }
        // 返回用户答案
        // 构造返回值，填充返回对象的信息
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        userAnswer.setResultScore(totalScore);
        return userAnswer;
    }
}
